<template>
	<view class="container">
			<Loading :isLoading="isLoading"></Loading>
		<uni-row class="container-pcp">
			<uni-col :span="10">
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="4">
						<view class="text-pcp">工单号:</view>
					</uni-col>
					<uni-col :span="16">
						<uni-easyinput trim="all" v-model="mo" placeholder="" @change="changeMo"></uni-easyinput>
					</uni-col>
					<uni-col :span="4">
						<yt-scanCode btnName="扫码" style="margin-left: 18%;margin-top: 3px;"
							@getScanCode="OnScanningCode"></yt-scanCode>
					</uni-col>
				</uni-row>
			</uni-col>
			<uni-col :span="9">
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="4">
						<view class="text-pcp">工序:</view>
					</uni-col>
					<uni-col :span="20">
						<zxz-uni-data-select v-model="jobId" collapse-tags dataKey="jobName" dataValue="jobId"
							:localdata="options" @change="changeCode"></zxz-uni-data-select>
					</uni-col>
				</uni-row>
			</uni-col>
			<uni-col :span="5">
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="24">
						<view class="num-pcp">数量: {{num}}</view>
					</uni-col>
				</uni-row>
			</uni-col>
		</uni-row>
		<uni-section title="检验结果" padding="0 0 0px 0px" type="line" titleFontSize="16px"
			style="background-color: #d6d6d6d7;">
			<template v-slot:right>
				<uni-row>
					<uni-col :span="4.5">
						<button type="primary" style="margin-right:20px;" size="mini" @click="synchronous">同步</button>
						<button type="primary" size="mini" @click="save">保存</button>
					</uni-col>
				</uni-row>
			</template>
		</uni-section>
		<uni-row class="container-pcp">
			<uni-col :span="12">
				<uni-row class="demo-uni-row" :width="nvueWidth">
					<uni-col :span="3">
						<view class="text-pcp">刻字号:</view>
					</uni-col>
					<uni-col :span="20">
						<zxz-uni-data-select v-model="Engraved" collapse-tags dataKey="mapaPatchcode"
							dataValue="mapaPatchcode" :localdata="EngravedList"
							@change="EngravedCode"></zxz-uni-data-select>
					</uni-col>
				</uni-row>
			</uni-col>
		</uni-row>
		<uni-table ref="table" :style="{ height: viewHeight+'px' }" :loading="loading" border stripe emptyText="暂无更多数据">
			<uni-tr>
				<uni-th width="50px" align="center">序号</uni-th>
				<uni-th width="90px" align="center">泡泡号</uni-th>
				<uni-th width="100px" align="center">测试项目</uni-th>
				<uni-th width="90px" align="center">刻字号</uni-th>
				<uni-th width="80px" align="center">标准值</uni-th>
				<uni-th width="80px" align="center">下误差</uni-th>
				<uni-th width="80px" align="center">上误差</uni-th>
				<uni-th align="center">留量尺寸/换算尺寸/批量量具</uni-th>
				<uni-th width="90px" align="center">工具型号</uni-th>
				<uni-th width="80px" align="center">最小值</uni-th>
				<uni-th width="80px" align="center">最大值</uni-th>
				<uni-th width="150px" align="center">实测值</uni-th>
			</uni-tr>
			<uni-tr v-for="(item, index) in tableData" :key="index">
				<uni-td align="center">{{index + 1+pageSize*(pageCurrent-1)}}</uni-td>
				<uni-td align="center">{{ item.chitName }}</uni-td>
				<uni-td align="center">{{ item.characteristics }}</uni-td>
				<uni-td align="center">{{ item.kzh }}</uni-td>
				<uni-td align="center">{{ item.chitStandardvalue }}</uni-td>
				<uni-td align="center">{{ item.chitNegativeerror }}</uni-td>
				<uni-td align="center">{{ item.chitPositiveerror }}</uni-td>
				<uni-td align="center">{{ item.reservedSize }}</uni-td>
				<uni-td align="center">{{ item.chitUseTools }}</uni-td>
				<uni-td align="center">{{ item.chitMin }}</uni-td>
				<uni-td align="center">{{ item.chitMax }}</uni-td>
				<uni-td v-show="item.chitValuetypeName!=='数值型'" align="center">
					<!-- <uni-data-select v-model="item.scValue"
						style="width: 120px;" :localdata="range" @change="change(item)"></uni-data-select> -->
					<uni-data-checkbox style="width: 120px;" mode="tag" v-model="item.scValue" :localdata="range"
						@change="change(item)"></uni-data-checkbox>

				</uni-td>
				<uni-td v-show="item.chitValuetypeName==='数值型'" align="center"> <uni-easyinput v-model="item.scValue"
						style="width: 120px;" placeholder="" @blur="changeValue(item)" @clear="changeValue(item)"
						@keyboardheightchange="changeValue(item)" />
				</uni-td>
			</uni-tr>
		</uni-table>
		<view class="uni-pagination-box"><uni-pagination show-icon :page-size="pageSize" :current="pageCurrent"
				:total="total" @change="pageChange" /></view>
		<sl-pda-employ :config="pdaConfig"></sl-pda-employ>
	</view>
</template>
<script>
	import Loading from '@/components/Loading.vue';
	import {
		listJobByPid,
		pcpGetMoList,
		getPcpListByProcId,
		savePcpRecord,
		pcpGetList,
		syncBomPcpData
	} from '@/api/system/pcp.js'
	import slPdaEmploy, {
		initPdaScan,
		manualTrigScan,
		onUnloadPdaScan,
		BroadcastConfig
	} from '../../uni_modules/sl-pdaEmploy'
	export default {
		components: {
			slPdaEmploy,
			Loading
		},
		data() {
			return {
				pdaConfig: { // 可以抽离出去管理这个配置
					BroadcastAction: 'com.speedata.showdecodedata',
					BroadcastLabelKey: 'message',
					BroadcastTriglKey: 'messagetype',
				},
				// 每页数据量
				pageSize: 10,
				// 当前页
				pageCurrent: 1,
				// 数据总量
				total: 0,
				loading: false,
				mo: "",
				jobId: "",
				num: 0,
				Engraved: "",
				tabs: ["暂无"],
				current: 0,
				nvueWidth: uni.getWindowInfo().screenWidth,
				options: [],
				tableData: [],
				tableList: [],
				EngravedList: [],
				isLoading: false,
				range: [{
						value: "NG",
						text: "NG"
					},
					{
						value: "PASS",
						text: "PASS"
					},
				],
				viewHeight: uni.getSystemInfoSync().windowHeight - 190, // 更新视口高度
			}
		},
		onLoad() {
			initPdaScan(code => {
				console.log(code)
				this.mo = code;
				this.SelectAll(code);
			})
		},
		onUnload() {
			onUnloadPdaScan()
		},
		methods: {
			// 同步
			async synchronous(){
				if(this.jobId===""){
					this.$modal.msgError("工序不能为空")
				}else{
				    await syncBomPcpData(this.jobId).then(res => {
					    this.SelectionData();
						this.$modal.msgSuccess("同步成功")
					})
				}
				
			},
			OnScanningCode(res) {
				this.mo = res;
				this.SelectAll(res);
			},
			changeMo() {
				this.SelectAll(this.mo);
			},
			SelectAll(mo) {
				if (mo !== "") {
					this.Engraved = "";
					this.jobId = "";
					this.num = 0;
					this.options = [];
					this.EngravedList = [];
					pcpGetMoList(mo.trim()).then(res => {
						console.log('-----------',res)
						let data = [];
						for (var i = 0; i < res.data.length; i++) {
							let a = {
								jobId: res.data[i].jobId,
								jobName: res.data[i].jobCode + " - " + res.data[i].jobName,
								jobPrudctnum: res.data[i].jobPrudctnum
							}
							data.push(a);
						}
						this.options = data;
					}).catch(error => {
						reject(error)
					})
					pcpGetList(mo.trim()).then(res => {
						console.log(res)
						this.EngravedList = res;
					}).catch(error => {
						reject(error)
					})
				}
			},
			changeCode() {
				// uni.showModal({
				// 	title: '数据变更',
				// 	content: '数据发生变更是否保存？',
				// 	success: (res) => {
				// 		if (res.confirm) {
				//			this.save();
				// 			this.SelectionData();
				// 		} else if (res.cancel) {
				// 			this.SelectionData();
				// 		}
				// 	}
				// });
				this.SelectionData();
			},
			SelectionData() {
				this.clear();
				for (var i = 0; i < this.options.length; i++) {
					if (this.options[i].jobId === this.jobId) {
						this.num = this.options[i].jobPrudctnum;
					}
				}
				if (this.num > 0 && this.EngravedList.length === 0) {
					for (var i = 0; i < this.num; i++) {
						if (i < 9) {
							this.EngravedList.push({
								mapaPatchcode: "0" + (i + 1)
							})
						} else {
							this.EngravedList.push({
								mapaPatchcode: i + 1
							})
						}
					}
				}
				if (this.jobId !== "") {
					this.loading = true
					getPcpListByProcId(this.jobId, "").then(res => {
						console.log(res.data)
						if (res.data.length > 0) {
							this.tableList = res.data;
							this.tableData = this.getData(1, this.tableList, this.Engraved);
							this.loading = false
						} else {
							this.loading = false
							this.$modal.msgError("暂无检验数据")
						}
					}).catch(error => {
						this.loading = false
						reject(error)
					})
				}
			},
			clear() {
				this.num = 0;
				this.pageSize = 10;
				this.pageCurrent = 1;
				this.total = 0;
				this.tableList = [];
				this.tableData = [];
			},
			pageChange(e) {
				this.pageCurrent = e.current;
				this.tableData = this.getData(e.current, this.tableList, this.Engraved);
			},
			getData(pageCurrent, table, Engraved) {
				if (Engraved !== "") {
					let tablefilter = [];
					for (var i = 0; i < table.length; i++) {
						if (table[i].kzh === Engraved) {
							tablefilter.push(table[i]);
						}
					}
					let data = tablefilter.filter((item, index) => {
						const idx = index - (pageCurrent - 1) * this.pageSize
						return idx < this.pageSize && idx >= 0
					})
					this.total = tablefilter.length;
					return data;
				}
			},
			EngravedCode() {
				console.log(this.Engraved)
				this.tableData = this.getData(this.pageCurrent, this.tableList, this.Engraved);
			},
			save() {
				console.log(this.tableList)
				savePcpRecord(this.tableList).then(res => {
					this.$modal.msgSuccess("保存成功")
				}).catch(error => {
					reject(error)
				})
			},
			changeValue(item) {
				for (var i = 0; i < this.tableList.length; i++) {
					if (this.tableList[i].prId === item.prId) {
						this.tableList[i].scValue = item.scValue
					}
				}
				//console.log(this.tableList)
			},
			change(item) {
				for (var i = 0; i < this.tableList.length; i++) {
					if (this.tableList[i].prId === item.prId) {
						this.tableList[i].scValue = item.scValue
					}
				}
				//console.log(this.tableList)
			},

		}
	}
</script>

<style>
	.container {
		background-color: #fff;
		margin-top: 0.1vh;
		height: 99.8vh;
		margin-bottom: 0.1vh;
	}

	.container-pcp {
		border: 0.1px solid #d1d1d1;

	}

	.demo-uni-row {
		padding-left: 10px;
		margin-top: 6px;
		margin-bottom: 6px;
	}

	.text-pcp {
		font-size: 16px;
		margin-top: 4px;
		float: right;
		margin-right: 6px;
	}

	.num-pcp {
		font-size: 16px;
		margin-top: 4px;
		float: left;
		margin-left: 20px;
	}
</style>